package com.wyh.domain.vo.groupOne.threemeterTree.manageTableThreemeter.utils;

import com.wyh.domain.po.groupOne.threemeterAbout.Threemeter;
import com.wyh.domain.vo.groupOne.threemeterTree.MasterDataUserThreemeterTableVO;
import com.wyh.domain.vo.groupOne.threemeterTree.manageTableThreemeter.ManageThreemeterVO;
import com.wyh.service.groupOne.threemeterAbout.threemeterTable.MasterDataIUserThreemeterTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class ThreemeterConverter {
    @Autowired
    private MasterDataIUserThreemeterTableService masterDataIUserThreemeterTableService;

    public ManageThreemeterVO convert(Threemeter threemeter) {
        Integer updateUser = threemeter.getUpdateUser();
        MasterDataUserThreemeterTableVO user;
        if (updateUser!=null){
//            System.out.println(threemeter.getUpdateUser());
            user = masterDataIUserThreemeterTableService.selectById(threemeter.getUpdateUser());
            if (threemeter.getMultiplier()==null){
                threemeter.setMultiplier(null);
            }
        }else {
            user = new MasterDataUserThreemeterTableVO("暂无最后修改人名字","");
        }
        if (threemeter.getMultiplier()==null){
            return ManageThreemeterVO.builder()
                    .id(threemeter.getId())
                    .nature(threemeter.getNature())
                    .infoName(threemeter.getInfoName())
                    .number(threemeter.getNumber())
                    .name(threemeter.getName())
                    .updateUser(user.getName())
                    .phoneNumber(user.getTelephone())
                    .time(threemeter.getUpdateTime())
                    .initial(threemeter.getInitial())
                    .code(threemeter.getCode())
                    .remarks(threemeter.getRemarks())
                    .build();
        }
        else {
            return ManageThreemeterVO.builder()
                    .id(threemeter.getId())
                    .nature(threemeter.getNature())
                    .infoName(threemeter.getInfoName())
                    .number(threemeter.getNumber())
                    .name(threemeter.getName())
                    .updateUser(user.getName())
                    .phoneNumber(user.getTelephone())
                    .multiplier(threemeter.getMultiplier().toString())
                    .time(threemeter.getUpdateTime())
                    .initial(threemeter.getInitial())
                    .code(threemeter.getCode())
                    .remarks(threemeter.getRemarks())
                    .build();
        }
    }

    public  List<ManageThreemeterVO> convertList(List<Threemeter> threemetertList) {
        return threemetertList.stream()
                .map(this::convert)
                .collect(Collectors.toList());
    }
}
